package com.rosesoft.module.mail;

import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.LineNumberReader;
import java.util.ArrayList;
import java.util.List;

import com.rosesoft.frame.utils.SendMail;

/**
 * @FileName SendMailService.java
 * @Description: 
 * @author rose
 * @version 1.0
 * <p>Company: ROSE-SOFT</p>
 * <p>Copyright: Copyright (c) 2011</p>
 */
public class SendMailService {
	
	private String emails = "";
	private String content = "";
	
	public String getEmails() {
		return emails; 
	}
	public void setEmails(String emails) {
		this.emails = emails;
	}
	public String getContent() {
		return content;
	}
	public void setContent(String content){
		this.content = content;
	}
	
	
	public static final String FILE_CONTENT_NAME = "content.html";
//	public static final String MAIL_LIST = "list0507.txt";
//	public static final String MAIL_LIST = "maillist.txt";
	public static final String MAIL_LIST = "fail.txt";
	public static final String SUBJECT = "CALL FOR PAPERS";
	public static void main(String[] args) {
		new SendMailService().sendMailToSoManyPeople();
//		List list = new ArrayList();
//		new SendForHui().setEmailAddrs(list);
//		System.out.println(list.size());
	}
	
	/**
	 * 发邮件至所有 挨个发送
	 * void
	 * @author rose
	 */
	public void sendMailToSoManyPeople(){
		SendMail sendMail = new SendMail();
		StringBuffer contentSb = new StringBuffer();
		List emailList = new ArrayList();
		contentSb.append(this.content);
		this.setEmailAddrs(emailList);//设定所有收件人列表
		System.out.println(emailList.size());
		StringBuffer successSb = new StringBuffer();
		StringBuffer failSb = new StringBuffer();
		List successList = new ArrayList();
		List failList = new ArrayList();
		sendMail.setSubject(SUBJECT);
		String emailAdd = "";
		List threadMailList = new ArrayList();
		ThreadGroup tg = new ThreadGroup("checkmailthread");
		int a = 1;
		int j = 0;
		int k = 0;
		for (int i = 0; i < emailList.size(); i++) {
			emailAdd = emailList.get(i) != null ? emailList.get(i).toString() : "";
			if (!emailAdd.trim().equals("")) {//如果有地址 放到一个list里面
				k++;
				threadMailList.add(emailAdd);
				if (k % 20 == 0) {//如果装载了300封邮件就开始开辟线程做处理
					System.out.println(k);
					MailThread mailThread = new MailThread(threadMailList,successList,failList,contentSb,successSb,failSb,sendMail,a);
					a++;
					Thread thread = new Thread(tg,mailThread);
					thread.start();//开始跑哦
					try {
						Thread.sleep(20000);//休息2秒
					} catch (InterruptedException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
					threadMailList = new ArrayList();
				}
			}
		}
		//小于300个时候
		MailThread mailThread = new MailThread(threadMailList,successList,failList,contentSb,successSb,failSb,sendMail,a);
		Thread thread = new Thread(tg,mailThread);
		thread.start();//开始跑哦
		
		CheckThread checkThread = new CheckThread(tg,successList,failList,successSb,failSb,sendMail);
		checkThread.start();
	}
	
	public void setContent(StringBuffer contentSb){
		InputStream is = null;
		InputStreamReader isr = null;
		LineNumberReader lnr = null;
		is = this.getClass().getResourceAsStream(FILE_CONTENT_NAME);
		isr = new InputStreamReader(is);
		lnr = new LineNumberReader(isr);
		String temp = "";
		try {
			while((temp = lnr.readLine()) != null){
				contentSb.append(temp);
			}
		} catch (IOException e) {
			e.printStackTrace();
		}finally{
			try {
				lnr.close();
				isr.close();
				is.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	}
	
	public void setEmailAddrs(List list){
		InputStream is = null;
		InputStreamReader isr = null;
		LineNumberReader lnr = null;
		is = this.getClass().getResourceAsStream(MAIL_LIST);
		isr = new InputStreamReader(is);
		lnr = new LineNumberReader(isr);
		String temp = "";
		try {
			while((temp = lnr.readLine()) != null){
				if (!temp.trim().equals("")) {
					list.add(temp.trim());
				}
			}
		} catch (IOException e) {
			e.printStackTrace();
		}finally{
			try {
				lnr.close();
				isr.close();
				is.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	}
}
